#include <iostream>
#include <sys/shm.h>
#include <unistd.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <fcntl.h>
#include <string.h>
#include <sys/select.h>
#include <signal.h>
#include "../../macro.h"

using std::cout;
using std::cerr;
using std::endl;

constexpr static int BUF_SIZE = 1024;

struct Protocol{
    int flag;
    char buf[BUF_SIZE];
};


Protocol *init_shm()
{
    int shmid = shmget(1000, sizeof(struct Protocol), IPC_CREAT | 0600);
    ERR_CHECK(shmid, -1, "shmget")

    struct Protocol *proto = (struct Protocol *)shmat(shmid, NULL, 0);
    ERR_CHECK(proto, (struct Protocol *)-1, "shmat")

    bzero(proto, sizeof(struct Protocol));
    return proto;
}

void run()
{
    // 创建 a - a1 的共享内存 
    struct Protocol *protol = init_shm();

    while(1){
        if(protol->flag){
            cout << protol->buf;
            protol->flag = 0;
            memset(protol->buf, 0, strlen(protol->buf));
        }
    }
}

int main(int args, char *argv[])
{
    run();
    return 0;
}